<?php
/**
 * 友情链接 分类
 * */
class FriendlinksclassAction extends Action
{

    /**
     * 输入对象
     * */
    //public $Input;

    public function _initialize()
    {
        //$Input = Input::getInstance();
        //$Input->filter('addslashes');
    }
	
    /**
     * 管理所有的分类
     * */
    public function AdminIndex()
    {
        EasyACL('Friendlinksclass','AdminIndex',$_SESSION);
        $Class = D('Friendlinksclass');
        $count = $Class->count(NULL,'cid'); //Count ALL Content
        $listRows = 15; //每页显示数量
        $fields = 'cid,name,created,updated'; //显示字段

        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $Class->findall(NULL,$fields,'cid desc',$p->firstRow . ',' .$p->listRows);
        $page = $p->show();
        
        //放入模板
        $this->assign('Class',$list);
        $this->assign('page',$page);
        $this->display();
    }

    /**
     * 创建分类
     * */
    public function AdminCreate()
    {
        EasyACL('Friendlinksclass','AdminCreate',$_SESSION);
        if ($this->isPost()) {
            $Class = D('Friendlinksclass');
            if ($Class->create()) {
                if ($Class->add()) {
                    $this->success('成功创建分类');
                } else {
                    $this->error($Class->getError());
                }
            } else {
                $this->error($Class->getError());
            }
        }
        $this->display();
    }

    /**
     * 修改分类名称
     * */
    public function AdminUpdate()
    {
        EasyACL('Friendlinksclass','AdminUpdate',$_SESSION);
        $Class = D('Friendlinksclass');
        $Input = Input::getInstance();
        $Input->filter('addslashes');
        if ($this->isPost()) {
            import('ORG.Util.HashMap');
            $conditions = new HashMap();
            $conditions->put('cid',array('EQ',$Input->post('cid')));
            $Class->where($conditions)->find();
            if ($Class->create()) {
                if ($Class->save()) {
                    $this->redirect('AdminIndex','Friendlinksclass');
                } else {
                    $this->error($Class->getError());
                }
            } else {
                $this->error($Class->getError());
            }
        }

        //获取要修改的分类
        import('ORG.Util.HashMap');
        $conditions = new HashMap();
        $conditions->put('cid',array('EQ',$Input->get('cid')));

        $ClassContent = $Class->where($conditions)->find();
        $this->assign('C',$ClassContent);
        $this->display();
    }

    /**
     * 删除单个
     * */
    public function AdminDel($cid)
    {
        EasyACL('Friendlinksclass','AdminDel',$_SESSION);
        $Class = D('Friendlinksclass');
        if ($Class->deleteById($cid)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 删除多个
     * */
    public function AdminDels()
    {
        EasyACL('Friendlinksclass','AdminDels',$_SESSION);
        //POST
        if ($this->isPost()) {
            $Cids = $_POST['cid'];
            $Error = '';
            foreach($Cids as $cid) {
                if (!$this->AdminDel($cid)) {
                    $Error .= '删除编号为' . $cid . '分类失败';
                }
            }
            if (empty($Error)) {
                $this->success('成功删除选中');
            } else {
                $this->error($Error);
            }
        }
        
        //GET
        if ($this->isGet()) {
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            if ($this->AdminDel($Input->get('cid'))) {
                $this->success('成功删除');
            } else {
                $this->error('尝试删除' . $Input->get('cid') . '分类失败');
            }
        }
    }
}
?>
